package com.ffox.service.impl;

import com.ffox.entity.HomeworkProblem;
import com.ffox.entity.HomeworkProblemFile;
import com.ffox.entity.HomeworkResult;
import com.ffox.entity.HomeworkResultFile;
import com.ffox.fastDfsUtil.FastDfsUtil;
import com.ffox.mapper.HomeworkMapper;
import com.ffox.service.HomeworkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 作业服务实现类，提供作业相关业务逻辑的实现。
 */
@Service
public class HomeworkServiceImpl implements HomeworkService {
    @Autowired
    private HomeworkMapper homeworkMapper;
    @Autowired
    private FastDfsUtil fastDfsUtil;

    /**
     * 插入一条新的作业题目，并上传相关附件。
     *
     * @param problem 作业题目对象，包含作业的基本信息
     * @param files   上传的文件数组
     * @return 操作结果标识，1表示成功
     */
    @Override
    public Integer insertHomeworkProblem(HomeworkProblem problem, MultipartFile[] files) {
        // 设置创建时间并插入作业题目
        problem.setCreatedAt(LocalDateTime.now());
        homeworkMapper.insertHomeworkProblem(problem);
        Integer problemId = homeworkMapper.getHomeworkProblemLastID();

        // 遍历上传的文件，保存到FastDFS并记录文件信息
        for (int i = 0; i < files.length; i++) {
            String fileName = files[i].getOriginalFilename();
            String afterLastDot = fileName.substring(fileName.lastIndexOf(".") + 1);
            String url = "";
            try {
                byte[] filesBytes = files[i].getBytes();
                url = fastDfsUtil.upload_file(filesBytes, afterLastDot);
            } catch (Exception e) {
                e.printStackTrace();
            }

            // 构建并保存作业题目文件信息
            HomeworkProblemFile homeworkProblemFile = new HomeworkProblemFile();
            homeworkProblemFile.setHomeworkProblemId(problemId);
            homeworkProblemFile.setFileName(fileName);
            homeworkProblemFile.setFileSize((int) files[i].getSize());
            homeworkProblemFile.setFileType(afterLastDot);
            homeworkProblemFile.setUploadTime(LocalDateTime.now());
            homeworkProblemFile.setFilePath(url);
            homeworkMapper.insertHomeworkProblemFile(homeworkProblemFile);
        }
        return 1;
    }

    /**
     * 根据用户编号获取该用户发布的所有作业题目。
     *
     * @param userNum 用户编号
     * @return 作业题目列表
     */
    @Override
    public List<HomeworkProblem> getHomeworkProblemByUserNum(String userNum) {
        return homeworkMapper.getHomeworkProblemByUserNum(userNum);
    }

    /**
     * 根据作业题目ID获取该题目的所有提交结果。
     *
     * @param problemId 作业题目ID
     * @return 作业结果列表
     */
    @Override
    public List<HomeworkResult> getHomeworkResultByProblemId(String problemId) {
        return homeworkMapper.getHomeworkResultByProblemId(problemId);
    }

    /**
     * 更新作业结果的评分、教师评语和批改时间。
     *
     * @param score     评分
     * @param tComment  教师评语
     * @param resultId  作业结果ID
     * @return 更新操作影响的行数
     */
    @Override
    public Integer updateHomeworkScoreAndTCommentAndCorrectTime(Long score, String tComment, Integer resultId) {
        LocalDateTime correctTime = LocalDateTime.now();
        return homeworkMapper.updateHomeworkScoreAndTCommentAndCorrectTime(score, tComment, correctTime, resultId);
    }

    /**
     * 根据作业结果ID获取该结果的所有附件文件。
     *
     * @param resultId 作业结果ID
     * @return 作业结果文件列表
     */
    @Override
    public List<HomeworkResultFile> getHomeworkResultFileByResultId(Long resultId) {
        return homeworkMapper.getHomeworkResultFileByResultId(resultId);
    }

    /**
     * 根据作业题目ID获取该题目的所有附件文件。
     *
     * @param problemId 作业题目ID
     * @return 作业题目文件列表
     */
    @Override
    public List<HomeworkProblemFile> getHomeworkProblemFileByProblemId(Integer problemId) {
        return homeworkMapper.getHomeworkProblemFileByProblemId(problemId);
    }

    /**
     * 根据班级ID获取该班级发布的所有作业题目。
     *
     * @param classId 班级ID
     * @return 作业题目列表
     */
    @Override
    public List<HomeworkProblem> getHomeworkProblemByClassId(Long classId) {
        return homeworkMapper.getHomeworkProblemByClassId(classId);
    }

    /**
     * 插入一条新的作业结果，并上传相关附件。
     *
     * @param result 作业结果对象，包含作业提交的基本信息
     * @param files  上传的文件数组
     * @return 操作结果标识，1表示成功
     */
    @Override
    public Integer insertHomeworkResult(HomeworkResult result, MultipartFile[] files) {
        // 设置提交时间并插入作业结果
        result.setSubmitTime(LocalDateTime.now());
        homeworkMapper.insertHomeworkResult(result);
        Integer resultId = homeworkMapper.getHomeworkResultLastID();

        // 遍历上传的文件，保存到FastDFS并记录文件信息
        for (int i = 0; i < files.length; i++) {
            String fileName = files[i].getOriginalFilename();
            String afterLastDot = fileName.substring(fileName.lastIndexOf(".") + 1);
            String url = "";
            try {
                byte[] filesBytes = files[i].getBytes();
                url = fastDfsUtil.upload_file(filesBytes, afterLastDot);
            } catch (Exception e) {
                e.printStackTrace();
            }

            // 构建并保存作业结果文件信息
            HomeworkResultFile homeworkResultFile = new HomeworkResultFile();
            homeworkResultFile.setHomeworkResultId(resultId);
            homeworkResultFile.setFileName(fileName);
            homeworkResultFile.setFileSize((int) files[i].getSize());
            homeworkResultFile.setFileType(afterLastDot);
            homeworkResultFile.setUploadTime(LocalDateTime.now());
            homeworkResultFile.setFilePath(url);
            homeworkMapper.insertHomeworkResultFile(homeworkResultFile);
        }

        return 1;
    }

    /**
     * 根据作业题目ID和学生编号获取该学生的作业提交结果。
     *
     * @param problemId   作业题目ID
     * @param studentNum  学生编号
     * @return 作业结果对象
     */
    @Override
    public HomeworkResult getHomeworkResult(Integer problemId, String studentNum) {
        return homeworkMapper.getHomeworkResult(problemId, studentNum);
    }
}
